/*
 * DA5c.c
 *
 * Created: 4/20/2015 6:36:12 PM
 *  Author: David Hunt
 */ 

//SERVO MOTOR
#include <avr/io.h>

int main(void)
{	
	ADCSRA = 0x87;		//ENABLE ADC AND PRESCALE CK/128
	ADMUX = 0xC0;		//2.56V VREF, ADC0 SINGLE ENDED INPUT, DATA RIGHT JUSTIFIED
	DDRC = 0;			//SET PORTC AS INPUT FOR ADC
	DDRB = 0xFF;		//SET PORTD AS OUTPUT
	
	//select non-inverted PWM, fast PWM with clk/8 prescale and ICR1 as top
	TCCR1A |= (1<< WGM11) | (1<<COM1A1) | (1<<COM1B1);
	TCCR1B |= (1<<CS11) | (1<<WGM12) | (1<<WGM13);
	ICR1 = 20000;		//50Hz PWM
	
	while(1)
	{
		ADCSRA |= (1<<ADSC);	//STARTING CONVERSION
		while((ADCSRA&(1<<ADIF))==0); //WAIT FOR CONVERSION TO FINISH
		OCR1A=ADCL;			//SET DUTYCYCLE BASED ON RESULT OF CONVERSION
	}
}